package engine.services.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.google.appengine.api.images.ImagesServiceFactory;

import engine.dataobjects.Image;
import engine.services.IImageService;

public class GoogleImageService implements IImageService {

	@Override
	public Image getImage(InputStream is) throws IOException {
		byte[] imageData = getBytes(is);
		com.google.appengine.api.images.Image img = ImagesServiceFactory.makeImage(imageData);
		Image image = new Image(img.getWidth(), img.getHeight());
		return image;
	}

	public static byte[] getBytes(InputStream is) throws IOException {
	    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

	    int len;
	    byte[] data = new byte[10000];
	    while ((len = is.read(data, 0, data.length)) != -1) {
	        buffer.write(data, 0, len);
	    }

	    buffer.flush();
	    return buffer.toByteArray();
	}
}
